/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package proyecto.servlets;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import proyecto.dao.TelefonoDAO;
import proyecto.dao.ClienteDAO;
import proyecto.modelo.Telefono;

/**
 *
 * @author supervisor
 */
public class TelefonoS extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        TelefonoDAO dao = new TelefonoDAO();
        Telefono te = new Telefono();
        String opcion = request.getParameter("opcion");
        if ("buscar".equals(opcion)) {
            String num = request.getParameter("tel_num");
            int idnum = dao.buscar(num);
            if (0 == idnum) {
                te.setNumero(num);
                dao.insertar(te);
                idnum = dao.buscar(num);
                request.setAttribute("idnum", idnum);
                RequestDispatcher rd = request.getRequestDispatcher("/oper_nc_ok.jsp");
                rd.forward(request, response);
            } else {
                ClienteDAO clidao = new ClienteDAO();
                int idcliente = clidao.buscarPorNumero(idnum);
                if (idcliente != 0) {
                    request.setAttribute("idnum", idnum);
                    //System.err.println(request.getAttribute("idnum"));
                    RequestDispatcher rd = request.getRequestDispatcher("/oper_nl_ok.jsp");
                    rd.forward(request, response);
                } else {
                    request.setAttribute("idnum", idnum);
                    RequestDispatcher rd = request.getRequestDispatcher("/oper_nc_ok.jsp");
                    rd.forward(request, response);
                }
            }

        } else {
            if ("insertar".equals(opcion)) {//Insertar
                te.setNumero(request.getParameter("tel_num"));
                dao.insertar(te);
                request.setAttribute("TELEFONO", "1");
                RequestDispatcher rd = request.getRequestDispatcher("/adm_tes_nuevo.jsp");
                rd.forward(request, response);
            } else {
                if ("modificar".equals(opcion)) {//Modificar
                    String numero = request.getParameter("tel_num");
                    int idtelefono = Integer.parseInt(String.valueOf(request.getParameter("tel_id")));
                    dao.modificar(idtelefono, numero);
                    RequestDispatcher rd = request.getRequestDispatcher("/adm_tes.jsp");
                    rd.forward(request, response);
                } else {//Eliminar
                    int idtelefono = Integer.parseInt(String.valueOf(request.getParameter("tel_id")));
                    dao.eliminar(idtelefono);
                    RequestDispatcher rd = request.getRequestDispatcher("/adm_tes.jsp");
                    rd.forward(request, response);
                }
            }

        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
